<?php
/**
 * Created by PhpStorm.
 * User: yuanxuncheng
 * Date: 2016-12-21
 * Time: 22:39
 */

namespace app\api\model\dao;

use app\library\CommonConfig;
use app\library\Flag;
use think\Model;
use think\Db;

class UserAccount extends Model
{
    //set talbename
    protected $table = 'user_account';
    //set primary key
    protected $pk = 'id';

    /**
     * @param $param
     * @return bool
     */
    public function addUserAccount($param)
    {
        if (empty($param['avatar'])) {
            $param['avatar'] = CommonConfig::DEFAULT_AVATAR;
        }
        $this->data($param);
        $ret = $this->save();
        if (false === $ret) {
            return false;
        }
        return $this->data[$this->pk];
    }

    /**
     * @param $param
     * @return false|int
     */
    public function updateUserAccount($param)
    {
        $extInfo = [];
        $user_info = Db::table($this->table)->where(['id' => $param['user_id']])->field('extend_info')->select();
        if (!empty($user_info)) {
            $user_info = $user_info[0];
            $extInfo = json_decode($user_info['extend_info'], true);
        }

        $field = array();
        if (isset($param['avatar'])) {
            $field['avatar'] = $param['avatar'];
        }
        if (isset($param['user_name'])) {
            $field['user_name'] = $param['user_name'];
        }
        if (isset($param['nick_name'])) {
            $field['nick_name'] = $param['nick_name'];
        }
        if (isset($param['phone'])) {
            $field['phone'] = $param['phone'];
        }
        if (isset($param['age'])) {
            $field['age'] = $param['age'];
        }

        if (isset($param['sex'])) {
            $field['sex'] = $param['sex'];
        }
        if (isset($param['flag'])) {
            $field['flag'] = $param['flag'];
        }
        if (isset($param['level'])) {
            $field['level'] = $param['level'];
        }
        if (isset($param['huanxin_username'])) {
            $field['huanxin_username'] = $param['huanxin_username'];
        }
        if (isset($param['constellation'])) {
            $extInfo['constellation'] = $param['constellation'];//星座
        }
        if (isset($param['dating_intention'])) {
            $extInfo['dating_intention'] = $param['dating_intention'];//交友意向
        }
        if (isset($param['marriage_status'])) {
            $extInfo['marriage_status'] = $param['marriage_status'];//婚姻状态
        }
        if (isset($param['tag'])) {
            $extInfo['tag'] = $param['tag'];//标签
            $tags = explode('||', trim($param['tag']));
            $label_color_count = count(Flag::$label_color);
            $labels = [];
            foreach ($tags as $tag) {
                if (1 == $label_color_count) {
                    $color = Flag::$label_color[0];
                } else if (0 < $label_color_count) {
                    $color = Flag::$label_color[mt_rand(0, $label_color_count - 1)];
                } else {
                    $color = '#272727';
                }
                $labels[] = [
                    'title' => $tag,
                    'color' => $color,
                ];
            }
            $field['label'] = json_encode($labels);
        }
        if (isset($param['location'])) {
            $extInfo['location'] = $param['location'];//所在地
        }
        if (isset($param['sexual_orientation'])) {
            $extInfo['sexual_orientation'] = $param['sexual_orientation'];//性取向
        }
        if (isset($param['home'])) {
            $extInfo['home'] = $param['home'];//家乡
        }
        if (isset($param['introduction'])) {
            $extInfo['introduction'] = $param['introduction'];//个人说明
        }
        if (isset($extInfo)) {
            $field['extend_info'] = json_encode($extInfo);
        }
        if (!empty($param['silence_time'])) {
            $field['silence_time'] = $param['silence_time'];
        }

        $field['update_time'] = time();

        $conds = array(
            'id' => $param['user_id'],
        );

        return $this->isUpdate(true)->save($field, $conds);
    }

    public function getUserAccount($param)
    {
        $conds = array(
            'id' => $param['user_id'],
        );
        return Db::table($this->table)->where($conds)->select();
    }

    public function getUserAccountByPhone($phone)
    {
        $conds = array(
            'phone' => $phone,
        );
        $field = 'user_name,nick_name,phone,flag,age,sex,avatar,level';
        return Db::table($this->table)->where($conds)->field($field)->select();
    }

    /**
     * 获取用户信息
     *
     */
    public function getUserAccountByUserids($userids, $fields = [])
    {
        $this->allowField(true);
        if (is_string($userids)) {
            $userids = explode(',', $userids);
        }

        $arrCond = [
            'id' => ['in', $userids]
        ];

        $arrField = [];
        foreach ($fields as $val) {
            if (in_array($val, $this->field)) {
                $arrField[] = $val;
            }
        }
        empty($arrField) && $arrField = $this->field;

        return $this->where($arrCond)->field($arrField)->select();
    }

    public function getUserAccountByConds($conds = [], $fields = [], $offset = 0, $limit = 10000, $orderby = '')
    {
        $this->allowField(true);

        $arrCond = [];
        foreach ($conds as $key => $val) {
            if (in_array($key, $this->field)) {
                $arrCond[$key] = $val;
            }
        }
        empty($arrCond) && $arrCond = '1=1';

        $arrField = [];
        foreach ($fields as $val) {
            if (in_array($val, $this->field)) {
                $arrField[] = $val;
            }
        }
        empty($arrField) && $arrField = $this->field;

        return Db::table($this->table)->where($arrCond)->limit($offset, $limit)->order($orderby)->field($arrField)->select();
    }

    /**
     * 获取用户信息
     *
     */
    public function getUserAccountByUsernames($usernames, $fields = [])
    {

        if (is_string($usernames)) {
            $usernames = explode(',', $usernames);
        }

        $arrCond = [
            'user_name' => ['in', $usernames]
        ];

        $arrField = [];
        foreach ($fields as $val) {
            if (in_array($val, $this->field)) {
                $arrField[] = $val;
            }
        }
        empty($arrField) && $arrField = $this->field;

        return Db::table($this->table)->where($arrCond)->field($arrField)->select();
    }

    /**
     * 获取用户信息
     *
     */
    public function getUserAccountByHuanxinUsernames($huanxin_usernames, $conds = [], $fields = [])
    {

        if (is_string($huanxin_usernames)) {
            $huanxin_usernames = explode(',', $huanxin_usernames);
        }

        $arrCond = [
            'huanxin_username' => ['in', $huanxin_usernames]
        ];

        if (!empty($conds)) {
            $arrCond = array_merge($arrCond, $conds);
        }

        $arrField = [];
        foreach ($fields as $val) {
            if (in_array($val, $this->field)) {
                $arrField[] = $val;
            }
        }
        empty($arrField) && $arrField = $this->field;

        return Db::table($this->table)->where($arrCond)->field($arrField)->select();
    }

    /**
     * @return mixed
     */
    public function getAllUserAccount()
    {
        $fields = ['id', 'user_name', 'nick_name', 'flag', 'phone', 'age', 'level', 'sex', 'reg_time', 'avatar'];
        //return  $this->where('flag',0)->field($fields)->paginate(10);
        return $this->paginate(10);
        //return  $this->all(['flag'=>0,'id'=>21]);
    }

    public function getFlagAttr($value)
    {
        $flag = [0 => '未知', 1 => '普通', 2 => 'VIP'];
        return $flag[$value];
    }

    public function getSexAttr($value)
    {
        $sex = [0 => '男', 1 => '女'];
        return $sex[$value];
    }

    public function getRegTimeAttr($value)
    {
        return date('y-m-d H:i:s', $value);
    }

    public function getSilenceTimeAttr($value)
    {
        if ($value == 0) {
            return '否';
        } else {
            return date('y-m-d H:i:s', $value);
        }
    }

}
